package com.example.demo.controller;

import com.example.demo.controller.requests.ModifyDrinkRequest;
import com.example.demo.model.Drink;
import com.example.demo.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/stu")
public class StudentController {
    // 新增
    @PostMapping("/add")
    public Student addNewStudent(@RequestBody Student student){
        Student addStudent = Student.builder().id(7L)
                .name("你的名字")
                .adminssionDate(LocalDate.of(2019,9,1))
                .major("移动应用开发")
                .build();
        return addStudent;
    }

    // 删除
    @DeleteMapping("/del/{id}")
    public String delectStudent(@PathVariable Long id){
        log.info(id+"删除成功");
        return "删除操作成功";
    }

    // 查询所有
        @GetMapping("/all")
        public List<Student> getAll(){
            List<Student> results=new ArrayList<>();
            results.add(Student.builder().id(1L)
                    .name("张三")
                    .adminssionDate(LocalDate.of(2019,9,1))
                    .major("移动应用开发")
                    .build());

            results.add(Student.builder().id(2L)
                    .name("李四")
                    .adminssionDate(LocalDate.of(2019,9,1))
                    .major("移动应用开发")
                    .build());

            results.add(Student.builder().id(3L)
                    .name("王五")
                    .adminssionDate(LocalDate.of(2019,9,1))
                    .major("软件技术")
                    .build());
            return results;
        }

    // 查询
        @GetMapping("/{id}")
        public Student getDrink (@PathVariable Long id) {
            // 后期从数据库查询   select  ……  where …… id=↑id
            Student addStudent = Student.builder().id(3L)
                    .name("王五")
                    .adminssionDate(LocalDate.of(2019,9,1))
                    .major("软件技术")
                    .build();
            return addStudent;
        }


    // 修改
        @PutMapping("/update")
        public Student modifyDrink(){
            Student addStudent = Student.builder().id(7L)
                    .name("陈冬")
                    .adminssionDate(LocalDate.of(2019,9,1))
                    .major("移动应用开发")
                    .build();
            return addStudent;
        }

    @PostMapping(value = "/batch",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Student> batchNewStudent(MultipartFile file) {
        List<Student> results=new ArrayList<>();
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line="";
            while ((line=reader.readLine())!=null){
                // 解析一行
                String[] args=line.split(",");
                Student student =Student.builder()
                        .id((long) Integer.parseInt(args[0]))
                        .name(args[1])
                        .adminssionDate(LocalDate.parse(args[2]))
                        .major(args[3])
                        .build();
                results.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }

}